{% load static %}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <link href="{% static 'blog/css/bootstrap.min.css' %}" rel="stylesheet">
    <link href="{% static 'blog/css/custom.css' %}" rel="stylesheet">
    <style>
        body {
            background-color: #f5f5f5;
            font-family: 'Helvetica Neue', Arial, sans-serif;
        }
        .register-container {
            max-width: 500px;
            margin: 60px auto;
            padding: 40px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        .register-title {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
            font-weight: 300;
        }
        .form-group {
            margin-bottom: 25px;
        }
        .form-control {
            height: 45px;
            border-radius: 4px;
            padding-left: 40px;
            transition: all 0.3s;
            border: 1px solid #ddd;
        }
        .form-control:focus {
            border-color: #4285f4;
            box-shadow: 0 0 0 2px rgba(66,133,244,0.2);
        }
        .form-control.is-invalid {
            border-color: #dc3545;
        }
        .form-control.is-invalid:focus {
            box-shadow: 0 0 0 2px rgba(220,53,69,0.2);
        }
        .invalid-feedback {
            color: #dc3545;
            font-size: 14px;
            margin-top: 5px;
        }
        .btn-register {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            background: #4285f4;
            border: none;
            margin-top: 20px;
        }
        .btn-register:hover {
            background: #3367d6;
        }
        .btn-register.loading {
            position: relative;
            color: transparent;
        }
        .btn-register.loading:after {
            content: "";
            position: absolute;
            width: 20px;
            height: 20px;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            border: 3px solid transparent;
            border-top-color: #ffffff;
            border-radius: 50%;
            animation: button-loading-spinner 1s ease infinite;
        }
        @keyframes button-loading-spinner {
            from { transform: rotate(0turn); }
            to { transform: rotate(1turn); }
        }
        .form-icon {
            position: relative;
        }
        .form-icon i {
            position: absolute;
            left: 15px;
            top: 12px;
            color: #999;
            font-size: 18px;
        }
        .alert {
            margin-bottom: 25px;
        }
        .login-link {
            text-align: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <h2 class="register-title">用户注册</h2>

        {% if form.errors %}
        <div class="alert alert-danger">
            {% for error in form.non_field_errors %}
                <p>{{ error }}</p>
            {% endfor %}
        </div>
        {% endif %}

        <form action="{% url 'blog:register' %}" method="post">
            {% csrf_token %}

            <div class="form-group form-icon">
                <i class="glyphicon glyphicon-user"></i>
                <input type="text" name="username" class="form-control" placeholder="用户名" required>
                {% if form.username.help_text %}
                <small class="form-text text-muted">{{ form.username.help_text }}</small>
                {% endif %}
            </div>

            <div class="form-group form-icon">
                <i class="glyphicon glyphicon-envelope"></i>
                <input type="email" name="email" class="form-control" placeholder="邮箱" required>
            </div>

            <div class="form-group form-icon">
                <i class="glyphicon glyphicon-lock"></i>
                <input type="password" name="password1" class="form-control" placeholder="密码" required>
                {% if form.password1.help_text %}
                <small class="form-text text-muted">{{ form.password1.help_text }}</small>
                {% endif %}
            </div>

            <div class="form-group form-icon">
                <i class="glyphicon glyphicon-lock"></i>
                <input type="password" name="password2" class="form-control" placeholder="确认密码" required>
            </div>

            <button type="submit" class="btn btn-primary btn-register">注册</button>

            <div class="login-link">
                已有账号？<a href="{% url 'login' %}">立即登录</a>
            </div>
        </form>
    </div>

    <script src="{% static 'blog/js/jquery-2.1.3.min.js' %}"></script>
    <script src="{% static 'blog/js/bootstrap.min.js' %}"></script>
    <script>
    $(document).ready(function() {
        $('form').on('submit', function(e) {
            var $btn = $('.btn-register');
            $btn.prop('disabled', true).addClass('loading');
            
            // 前端验证
            var isValid = true;
            $('input[required]').each(function() {
                if (!$(this).val()) {
                    $(this).addClass('is-invalid');
                    isValid = false;
                }
            });
            
            if (!isValid) {
                $btn.prop('disabled', false).removeClass('loading');
                return false;
            }
        });
        
        // 输入时移除错误状态
        $('input').on('input', function() {
            $(this).removeClass('is-invalid');
        });
    });
    </script>
</body>
</html>
